package com.example.demo.controller;

import com.example.demo.pojo.ModelInfo;
import com.example.demo.service.IModelService;
import com.example.demo.util.PageInfo;
import com.example.demo.util.PageUtil;
import com.example.demo.util.ResultData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
//import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ModelQuery;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 模型操作
 * @author Administrator
 */
@RestController
@RequestMapping("/model")
@Api("模型操作")
public class ModelDesignController implements ModelDataJsonConstants {
    protected static final Logger LOGGER = LoggerFactory.getLogger(ModelDesignController.class);

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private IModelService iModelService;

    @Autowired
    private ObjectMapper objectMapper;


    /**
     * 根据模型id查询
     * @param modelId
     * @return
     */
    @GetMapping(value="/{modelId}/json", produces = "application/json")
    @ApiOperation("根据模型id查询")
    public ObjectNode getEditorJson(@PathVariable String modelId) {
        ObjectNode modelNode = null;


        Model model = repositoryService.getModel(modelId);

        if (model != null) {
            try {
                if (StringUtils.isNotEmpty(model.getMetaInfo())) {
                    modelNode = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
                } else {
                    modelNode = objectMapper.createObjectNode();
                    modelNode.put(MODEL_NAME, model.getName());
                }
                modelNode.put(MODEL_ID, model.getId());
                ObjectNode editorJsonNode = (ObjectNode) objectMapper.readTree(
                        new String(repositoryService.getModelEditorSource(model.getId()), "utf-8"));
                modelNode.put("model", editorJsonNode);

            } catch (Exception e) {
                LOGGER.error("Error creating model JSON", e);
                throw new ActivitiException("Error creating model JSON", e);
            }
        }
        return modelNode;
    }

    /**
     * 根据模型id查询
     * @param modelInfo
     * @return
     */
    @PostMapping("/findModel")
    @ApiOperation("模型信息查询")
    public ResultData getEditorJson1(@RequestBody ModelInfo modelInfo, Integer pageNum, Integer pageSize) {
        return iModelService.pageByIdNameKeyModel(modelInfo, pageNum, pageSize);
    }


    @RequestMapping(value="/{modelId}/save", method = RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.OK)
    @ApiOperation("模型保存")
    public ResultData saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml) {
        return iModelService.saveModel(modelId, name, description, json_xml, svg_xml);
    }

}
